﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gift.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALBanner
    /// </summary>
    public partial class DALBanner
    {
        public DALBanner()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int flashid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbBanner");
            strSql.Append(" where flashid=@flashid");
            SqlParameter[] parameters = {
					new SqlParameter("@flashid", SqlDbType.Int,4)
			};
            parameters[0].Value = flashid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbBanner model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbBanner(");
            strSql.Append("flashname,flashpic,flashorder,flashurl,companyid,classId,position)");
            strSql.Append(" values (");
            strSql.Append("@flashname,@flashpic,@flashorder,@flashurl,@companyid,@classId,@position)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@flashname", SqlDbType.NVarChar,250),
					new SqlParameter("@flashpic", SqlDbType.NVarChar,250),
					new SqlParameter("@flashorder", SqlDbType.Int,4),
					new SqlParameter("@flashurl", SqlDbType.NVarChar,250),
					new SqlParameter("@companyid", SqlDbType.Int,4),
					new SqlParameter("@classId", SqlDbType.Int,4),
					new SqlParameter("@position", SqlDbType.VarChar,50)};
            parameters[0].Value = model.flashname;
            parameters[1].Value = model.flashpic;
            parameters[2].Value = model.flashorder;
            parameters[3].Value = model.flashurl;
            parameters[4].Value = model.companyid;
            parameters[5].Value = model.classId;
            parameters[6].Value = model.position;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbBanner model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbBanner set ");
            strSql.Append("flashname=@flashname,");
            strSql.Append("flashpic=@flashpic,");
            strSql.Append("flashorder=@flashorder,");
            strSql.Append("flashurl=@flashurl,");
            strSql.Append("companyid=@companyid,");
            strSql.Append("classId=@classId,");
            strSql.Append("position=@position");
            strSql.Append(" where flashid=@flashid");
            SqlParameter[] parameters = {
					new SqlParameter("@flashname", SqlDbType.NVarChar,250),
					new SqlParameter("@flashpic", SqlDbType.NVarChar,250),
					new SqlParameter("@flashorder", SqlDbType.Int,4),
					new SqlParameter("@flashurl", SqlDbType.NVarChar,250),
					new SqlParameter("@companyid", SqlDbType.Int,4),
					new SqlParameter("@classId", SqlDbType.Int,4),
					new SqlParameter("@position", SqlDbType.VarChar,50),
					new SqlParameter("@flashid", SqlDbType.Int,4)};
            parameters[0].Value = model.flashname;
            parameters[1].Value = model.flashpic;
            parameters[2].Value = model.flashorder;
            parameters[3].Value = model.flashurl;
            parameters[4].Value = model.companyid;
            parameters[5].Value = model.classId;
            parameters[6].Value = model.position;
            parameters[7].Value = model.flashid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int flashid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbBanner ");
            strSql.Append(" where flashid=@flashid");
            SqlParameter[] parameters = {
					new SqlParameter("@flashid", SqlDbType.Int,4)
			};
            parameters[0].Value = flashid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string flashidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbBanner ");
            strSql.Append(" where flashid in (" + flashidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbBanner GetModel(int flashid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 flashid,flashname,flashpic,flashorder,flashurl,companyid,classId,position from tbBanner ");
            strSql.Append(" where flashid=@flashid");
            SqlParameter[] parameters = {
					new SqlParameter("@flashid", SqlDbType.Int,4)
			};
            parameters[0].Value = flashid;

            Gift.Model.tbBanner model = new Gift.Model.tbBanner();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["flashid"] != null && ds.Tables[0].Rows[0]["flashid"].ToString() != "")
                {
                    model.flashid = int.Parse(ds.Tables[0].Rows[0]["flashid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["flashname"] != null && ds.Tables[0].Rows[0]["flashname"].ToString() != "")
                {
                    model.flashname = ds.Tables[0].Rows[0]["flashname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["flashpic"] != null && ds.Tables[0].Rows[0]["flashpic"].ToString() != "")
                {
                    model.flashpic = ds.Tables[0].Rows[0]["flashpic"].ToString();
                }
                if (ds.Tables[0].Rows[0]["flashorder"] != null && ds.Tables[0].Rows[0]["flashorder"].ToString() != "")
                {
                    model.flashorder = int.Parse(ds.Tables[0].Rows[0]["flashorder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["flashurl"] != null && ds.Tables[0].Rows[0]["flashurl"].ToString() != "")
                {
                    model.flashurl = ds.Tables[0].Rows[0]["flashurl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["companyid"] != null && ds.Tables[0].Rows[0]["companyid"].ToString() != "")
                {
                    model.companyid = int.Parse(ds.Tables[0].Rows[0]["companyid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["classId"] != null && ds.Tables[0].Rows[0]["classId"].ToString() != "")
                {
                    model.classId = int.Parse(ds.Tables[0].Rows[0]["classId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["position"] != null && ds.Tables[0].Rows[0]["position"].ToString() != "")
                {
                    model.position = ds.Tables[0].Rows[0]["position"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select flashid,flashname,flashpic,flashorder,flashurl,companyid,classId,position ");
            strSql.Append(" FROM tbBanner ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" flashid,flashname,flashpic,flashorder,flashurl,companyid,classId,position ");
            strSql.Append(" FROM tbBanner ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbBanner ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.flashid desc");
            }
            strSql.Append(")AS Row, T.*  from tbBanner T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbBanner";
            parameters[1].Value = "flashid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

